package net.oschina.app.ui;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.FrameLayout;

import net.oschina.app.R;
import net.oschina.app.fragment.favorite.FansFragment;
import net.oschina.app.fragment.favorite.FavoriteFragment;
import net.oschina.app.fragment.favorite.MsgFragment;

/**
 * Created by YL on 2017/4/2.
 */

public class BaseCommonTABActivity extends AppCompatActivity {
    private Toolbar tabvptoolbar;
    private android.widget.FrameLayout tabvpfl;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.base_tool_tabvp);
        this.tabvpfl = (FrameLayout) findViewById(R.id.tabvp_fl);
        this.tabvptoolbar = (Toolbar) findViewById(R.id.tabvp_toolbar);

        Intent intent = getIntent();

        //设置回退
        tabvptoolbar.setNavigationIcon(R.drawable.actionbar_back_icon_normal);
        //设置title
        String title = intent.getStringExtra("title");
        tabvptoolbar.setTitle(title);
        tabvptoolbar.setTitleTextColor(Color.WHITE);

        setSupportActionBar(tabvptoolbar);
        //设置监听
        tabvptoolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        String type = intent.getStringExtra("type");
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        if("fans".equals(type)) {
            FansFragment fansFragment = new FansFragment();
            setArg(intent, fansFragment);
            transaction.replace(R.id.tabvp_fl,fansFragment, "fans");
            transaction.commit();
        }else if("favorite".equals(type)){
            transaction.replace(R.id.tabvp_fl,new FavoriteFragment(), "favorite");
            transaction.commit();
        }else if("msg".equals(type)){
            transaction.replace(R.id.tabvp_fl,new MsgFragment(), "msg");
            transaction.commit();
        }
    }

    private void setArg(Intent intent, FansFragment fansFragment) {
        Bundle args = new Bundle();
        args.putInt("index", intent.getIntExtra("index", 0));
        fansFragment.setArguments(args);
    }

}
